#! /bin/bash
# -*- tab-width: 4; -*-
# vi: set sw=2 ts=4:

# Regenerate the reference ktx2 files for load tests, etc.

# Copyright 2017 The Khronos Group Inc.
# SPDX-License-Identifier: Apache-2.0

# Depth of this script relative to the project root
depth=../..

# Change dir to the testimages folder, the script location...
cd $(dirname $0)

# Ensure generation is not polluted by user environment
unset TOKTX_OPTIONS

function usage() {
  echo "Usage: $0 [--bindir <path/to/tools/bindir>]"
  exit 1
}

function check_exec() {
  if [ -z "$1" -o ! -x "$1" ]; then
    echo "$0: $1 not found."
    exit 1
  fi
}

if [ "$1" = "--bindir" ]; then
  shift
  if [ $# -eq 1 ]; then
    bindir=$1
    toktx=$bindir/toktx
    ktx2ktx2=$bindir/ktx2ktx2
    shift
    check_exec $toktx
    check_exec $ktx2ktx2
  else
    usage
  fi
fi

if [ $# -gt 0 ]; then
  usage
fi

function check_path() {
  if which $1 >/dev/null; then
    #declare -g $1="$1"  # For recent versions of bash.
    eval "$1=\$1"
  else
    echo "$1 not found in $PATH."
    exit 1
  fi
}

if [ -z "$toktx" ]; then
  check_path toktx
fi

if [ -z "$ktx2ktx2" ]; then
  check_path ktx2ktx2
fi

# Generate some reference files for toktx-tests ...

$ktx2ktx2 --test -f -o orient-down-metadata-u.ktx2 orient-down-metadata.ktx
$ktx2ktx2 --test -f -o orient-up-metadata-u.ktx2 orient-up-metadata.ktx
$ktx2ktx2 --test -f pattern_02_bc2.ktx
$ktx2ktx2 --test -f -o rgba-reference-u.ktx2 rgba-reference.ktx
$ktx2ktx2 --test -f texturearray_astc_8x8_unorm.ktx
$ktx2ktx2 --test -f texturearray_bc3_unorm.ktx
$ktx2ktx2 --test -f texturearray_etc2_unorm.ktx
$toktx --test --t2 --mipmap rgb-mipmap-reference-u.ktx2 ../srcimages/level0.ppm ../srcimages/level1.ppm ../srcimages/level2.ppm ../srcimages/level3.ppm ../srcimages/level4.ppm ../srcimages/level5.ppm ../srcimages/level6.ppm
$toktx --test --bcmp FlightHelmet_baseColor_basis.ktx2  ../srcimages/FlightHelmet_baseColor.png
$toktx --test --bcmp alpha_simple_basis.ktx2 ../srcimages/alpha_simple.png
$toktx --test --bcmp kodim17_basis.ktx2 ../srcimages/kodim17.png
$toktx --test --bcmp color_grid_basis.ktx2 ../srcimages/color_grid.png
$toktx --test --t2 --convert_oetf srgb g03n2c08.ktx2 ../srcimages/g03n2c08.png

# ... and additional files for the load tests.

$toktx --test --lower_left_maps_to_s0t0 rgb-reference-metadata.ktx ../srcimages/rgb.ppm

# ... the files with the KTX icons too, but this script only works with my
# arrangement of workareas, so keep commented out.
#./genktx2icons

$toktx --test --cubemap --genmipmap --bcmp cubemap_yokohama_basis_rd.ktx2 @@../srcimages/Yokohama3/filelist.txt

# ... this also but it takes a long time to generate especially with
# the debug version.
$toktx --test --cubemap -scale 0.5 --genmipmap --threads 1 --uastc --uastc_rdo_l 4 --zcmp 5 cubemap_goldengate_uastc_rdo4_zstd5_rd.ktx2 @@../srcimages/GoldenGateBridge3/filelist.txt
